package by.yakusekmaxim.bookgid.webapp.page.admin.user;

import java.util.Date;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.markup.html.form.SubmitLink;

import by.yakusekmaxim.bookgid.datamodel.UserAccount;
import by.yakusekmaxim.bookgid.datamodel.enums.UserRole;
import by.yakusekmaxim.bookgid.datamodel.enums.UserStatus;
import by.yakusekmaxim.bookgid.services.UserAccountService;
import by.yakusekmaxim.bookgid.webapp.page.AdminLayout;
import by.yakusekmaxim.bookgid.webapp.page.utils.renderer.UserRoleRenderer;
import by.yakusekmaxim.bookgid.webapp.page.utils.renderer.UserStatusRenderer;

public class EditUserPage extends AdminLayout {

	@Inject
	private UserAccountService userAccountService;

	protected void onInitialize() {
		super.onInitialize();
	}

	public EditUserPage(final UserAccount user) {

		UserAccount userAccount = user;

		if (userAccount.getId() == null) {
			add(new Label("pageNameLabel", new ResourceModel("p.admin.edituser.newname")));
		}else {
			add(new Label("pageNameLabel", getPageTitle()));
		}
		

		if (userAccount.getCreated() == null) {
			userAccount.setCreated(new Date());
		}

		Form<UserAccount> form = new Form<UserAccount>("form",
				new CompoundPropertyModel<UserAccount>(userAccount));
		final Label tfId = new Label("id", userAccount.getId());
		form.add(tfId);

		final Label tfCreated = new Label("created", userAccount.getCreated());
		form.add(tfCreated);

		final TextField<String> tfLastName = new TextField<String>("lastName");
		tfLastName.add(new PropertyValidator<String>());
		form.add(tfLastName);

		final TextField<String> tfFirstName = new TextField<String>("firstName");
		tfFirstName.add(new PropertyValidator<String>());
		form.add(tfFirstName);

		final TextField<String> tfMiddleName = new TextField<String>(
				"middleName");
		tfMiddleName.add(new PropertyValidator<String>());
		form.add(tfMiddleName);

		final TextField<String> tfEmail = new TextField<String>("email");
		tfEmail.add(new PropertyValidator<String>());
		form.add(tfEmail);

		final TextField<String> tfPassword = new TextField<String>("password");
		tfPassword.add(new PropertyValidator<String>());
		form.add(tfPassword);

		List<UserStatus> choicesStatus = Arrays.asList(UserStatus.values());
		form.add(new DropDownChoice<UserStatus>("status", choicesStatus,
				UserStatusRenderer.INSTANCE));

		List<UserRole> choicesRole = Arrays.asList(UserRole.values());
		form.add(new DropDownChoice<UserRole>("role", choicesRole,
				UserRoleRenderer.INSTANCE));

		form.add(new SubmitLink("cancel") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				UserPage page = new UserPage();
				setResponsePage(page);
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				userAccountService.saveOrUpdate(user);

				UserPage page = new UserPage();

//				page.error("Custom error");
				page.info("Профиль ID: " + user.getId() + " сохранён!");
//				page.warn("Custom warn");

				setResponsePage(page);
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		add(form);
	}

	@Override
	protected WebMarkupContainer getTarget() {
		return targetUser;
	}
	
	@Override
	protected IModel<String> getPageTitle() {
		return new ResourceModel("p.admin.edituser.editname");
	}

}
